﻿using UnityEngine;
using System.Collections;
using ProgressBar;

public class CARItem : MonoBehaviour {

	public int 	m_nID;
	public UILabel 	m_ItemLabel;
	public UITexture m_WaitingBar;
	public UITexture m_Texture;
	bool m_bLoaded;
	WWW  www;

	// Use this for initialization
	void Start () {
		m_bLoaded = false;
	}
	
	// Update is called once per frame
	void Update () {
		if(www != null)
			m_WaitingBar.GetComponent<ProgressRadialBehaviour> ().SetFillerSize (www.progress);

		if (m_bLoaded)
			m_WaitingBar.gameObject.SetActive(false);
	}

	void OnClick()
	{
		JsonDataRead.Instance.curARID = m_nID;
		CSceneManager.Instance.LoadScene ("ARLibrary", "ARBuyScene");
	}

	public IEnumerator SetTexture()
	{
		m_ItemLabel.text = JsonDataRead.Instance.arData [m_nID].sName;
		Texture tex = JsonDataRead.Instance.FindTextureByUrl (JsonDataRead.Instance.arData [m_nID].sMenuImageFilepath);

		if (tex == null) {
			www = new WWW (JsonDataRead.Instance.DataPath + JsonDataRead.Instance.arData [m_nID].sMenuImageFilepath);
			yield return www;
			m_Texture.mainTexture = www.texture;
			JsonDataRead.Instance.AddTexture(JsonDataRead.Instance.arData [m_nID].sMenuImageFilepath, www.texture);
			m_bLoaded = true;
		} else {
			m_Texture.mainTexture = tex;
			m_bLoaded = true;
		}
	}
}